<?php
if (!defined('__BACKEND__')) die ("you cannot directly access this page");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

global $action;
function search_content(){
    global $db, $q,$page,$pagesize;
if (empty($page)){
    $start=0;
} else {
    $start=$page*$pagesize;
}
if (!empty($q)){
    $word=explode(" ", strtolower($q));
    for ($i=0;$i<count($word);$i++){
        if(!is_word_exist($word[$i])){
            $word_id[$i]=insert_word($word[$i]);    
        } else $word_id[$i]=get_word_id ($word[$i]);
        
    }
   $selection=implode(',',$word_id); 
   //echo "$selection";
   search_event($selection);
   $sql="select distinct a.post_id,b.title,b.excerpt,b.contents,b.user_creator,b.date_create,b.post_category 
    from _search_index a,_post_contents b
   where a.post_id=b.id and a.word_id in ($selection)
   order by b.date_create desc limit $start,$pagesize";
   $result=$db->query($sql);
   $nrow=mysql_num_rows($result);
   
   if ($nrow>0){
       $pcount=floor($nrow/$pagesize);
        echo "<div class='search_header'>Ditemukan $nrow berita ...</div>";
       while ($row=$db->fetch_array($result)){
           extract($row);
           $fullname=get_user_info_id($user_creator, 'fullname');
           $category=get_post_category($post_category);
           $description=keyword_case($contents, $q);
           $words=explode(' ',$q);
           $pos=strpos($description,'<strong>',2);
           $description="...".substr($description,$pos);
           echo "<div class='search_detail'>
           <div class='search_title'><a href=\"?mod=posting&action=detail&post_id=$post_id\">$title</a></div>
           <div class='search_description'>$description</div>
           <div class='search_info'>$date_create, by $fullname in $category</div>
           </div>";
       }
       if ($pcount>1){
           echo "<div id='search_page'>";
           for ($i=0;$i<$pcount;$i++){
               echo "";
           }
           echo "</div>";
       }
   } else {
       echo "<div class='search_header'>Tidak ada posting dengan kata kunci $q </div>";
       
   }
}
}
function keyword_case($content,$words){
    $word=explode(' ',$words);
    $result=$content;
    if (is_array($word)){
    for ($i=0;$i<count($word);$i++){
        $replace="<font size='+1'><strong>{$word[$i]}</strong></font>";
        $result= str_replace($word, $replace, $result);
    }
    } else {
        $replace="<font size='+1'><strong>$words</strong></font>";
        $result= str_replace($words, $replace, $content);
    }
    return $result;
}
function search_event($selection){
    global $db,$pagesize,$page,$last_event_id,$q;
    if (empty($page)){
    $start=0;
} else {
    $start=$page*$pagesize;
}
if (!empty($selection)){
  
    
   $sql="select distinct b.* from _event_index a,_event_events b
    where a.word_id in ($selection) and a.event_id=b.id
   order by event_create desc limit $start,$pagesize";
    
   $result=$db->query($sql);
   $nrow=mysql_num_rows($result);
   
   if ($nrow>0){
       $pcount=floor($nrow/$pagesize);
       $i=0;
        echo "<div class='search_header'>Ditemukan $nrow event</div>";
       while ($row=$db->fetch_array($result)){
           extract($row);
           if ($nrow>1){
           $event_id[$i]=$id;
           } else {
               $event_id=$id;
           }
           $fullname=get_user_info_id($event_creator, 'fullname');
           $category=get_event_category($event_category);
           $description=keyword_case($event_content, $q);
           echo "<div class='search_detail'>
           <div class='search_title'><a href=\"?mod=events&action=show&id=$id\">$event_title</a></div>
           <div class='search_description'>$event_content</div>
           <div class='search_info'>$event_create, by $fullname in $category</div>
           </div>";
           $i++;
       }
       $_SESSION['last_event_id']=$event_id;
       if ($pcount>1){
           echo "<div id='search_page'>";
           for ($i=0;$i<$pcount;$i++){
               echo "";
           }
           echo "</div>";
       }
   } else {
      echo "<div class='search_header'>Tidak ada event dengan kata kunci $q </div>";
   }
   
}
}
switch($action){
    default:
        search_content();
        
}
?>
